﻿Imports System.Data.SqlClient
Public Class clsCatalogoRutas
    Private mintIdRuta As Integer
    Private mstrNombre As String
    Private mdblKilometros As Double
    Private mintcidValor01 As Integer
    Private mstrcCodigoV01 As String
    Private mintcidOpto As Integer
    Private mstrImagen As String
    Private mdblImporte01 As Double
    Private mdblImporte02 As Double
    Private mdblImporte03 As Double
    Private mdblImporte04 As Double
    Private mdblImporte05 As Double
    Private mstrTexto01 As String
    Private mstrTexto02 As String
    Private mstrTexto03 As String
    Private mstrTexto04 As String
    Private mstrTexto05 As String
    Public Property IdRuta As Integer
        Get
            Return mintIdRuta
        End Get
        Set(value As Integer)
            mintIdRuta = value
        End Set
    End Property
    Public Property Nombre As String
        Get
            Return mstrNombre
        End Get
        Set(value As String)
            mstrNombre = value
        End Set
    End Property
    Public Property Kilometros As Double
        Get
            Return mdblKilometros
        End Get
        Set(value As Double)
            mdblKilometros = value
        End Set
    End Property
    Public Property cidValor01 As Integer
        Get
            Return mintcidValor01
        End Get
        Set(value As Integer)
            mintcidValor01 = value
        End Set
    End Property
    Public Property cCodigoV01 As String
        Get
            Return mstrcCodigoV01
        End Get
        Set(value As String)
            mstrcCodigoV01 = value
        End Set
    End Property
    Public Property cidOpto As Integer
        Get
            Return mintcidOpto
        End Get
        Set(value As Integer)
            mintcidOpto = value
        End Set
    End Property
    Public Property Imagen As String
        Get
            Return mstrImagen
        End Get
        Set(value As String)
            mstrImagen = value
        End Set
    End Property
    Public Property Importe01 As Double
        Get
            Return mdblImporte01
        End Get
        Set(value As Double)
            mdblImporte01 = value
        End Set
    End Property
    Public Property Importe02 As Double
        Get
            Return mdblImporte02
        End Get
        Set(value As Double)
            mdblImporte02 = value
        End Set
    End Property
    Public Property Importe03 As Double
        Get
            Return mdblImporte03
        End Get
        Set(value As Double)
            mdblImporte03 = value
        End Set
    End Property
    Public Property Importe04 As Double
        Get
            Return mdblImporte04
        End Get
        Set(value As Double)
            mdblImporte04 = value
        End Set
    End Property
    Public Property Importe05 As Double
        Get
            Return mdblImporte05
        End Get
        Set(value As Double)
            mdblImporte05 = value
        End Set
    End Property
    Public Property Texto01 As String
        Get
            Return mstrTexto01
        End Get
        Set(value As String)
            mstrTexto01 = value
        End Set
    End Property
    Public Property Texto02 As String
        Get
            Return mstrTexto02
        End Get
        Set(value As String)
            mstrTexto02 = value
        End Set
    End Property
    Public Property Texto03 As String
        Get
            Return mstrTexto03
        End Get
        Set(value As String)
            mstrTexto03 = value
        End Set
    End Property
    Public Property Texto04 As String
        Get
            Return mstrTexto04
        End Get
        Set(value As String)
            mstrTexto04 = value
        End Set
    End Property
    Public Property Texto05 As String
        Get
            Return mstrTexto05
        End Get
        Set(value As String)
            mstrTexto05 = value
        End Set
    End Property
    Public Sub New()
        mintIdRuta = 0
        mstrNombre = ""
        mdblKilometros = 0
        mintcidValor01 = 0
        mstrcCodigoV01 = ""
        mintcidOpto = 0
        mstrImagen = ""
        mdblImporte01 = 0
        mdblImporte02 = 0
        mdblImporte03 = 0
        mdblImporte04 = 0
        mdblImporte05 = 0
        mstrTexto01 = ""
        mstrTexto02 = ""
        mstrTexto03 = ""
        mstrTexto04 = ""
        mstrTexto05 = ""
    End Sub
    Public Sub Guardar()
        If IdRuta = 0 Then
            Exit Sub
        End If

        Dim intRetVal As Integer = BuscarIntDatoCitra("Rutas", "IdRuta", "IdRuta=" & IdRuta)

        If intRetVal = 0 Then
            Insertar()
        Else
            Actualizar()
        End If
    End Sub
    Public Sub GuardaPorNombre()
        If Nombre = "" Then
            Exit Sub
        End If

        Dim intRetVal As Integer = BuscarIntDatoCitra("Rutas", "IdRuta", "Nombre='" & Nombre & "'")

        If intRetVal = 0 Then
            Insertar()
        Else
            Actualizar()
        End If
    End Sub
    Public Sub Buscar()
        Dim cnConn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = "SELECT * FROM Rutas WHERE Nombre='" & Nombre & "'"

            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            If rdBuscar.Read() Then
                mdblKilometros = rdBuscar("Kilometros")
                mintcidValor01 = rdBuscar("cidValor01")
                mstrcCodigoV01 = rdBuscar("cCodigoV01").ToString.Trim
                mintcidOpto = rdBuscar("cidOpto")
                mstrImagen = rdBuscar("Imagen").ToString.Trim
                mdblImporte01 = rdBuscar("Importe01")
                mdblImporte02 = rdBuscar("Importe02")
                mdblImporte03 = rdBuscar("Importe03")
                mdblImporte04 = rdBuscar("Importe04")
                mdblImporte05 = rdBuscar("Importe05")
                mstrTexto01 = rdBuscar("Texto01").ToString.Trim
                mstrTexto02 = rdBuscar("Texto02").ToString.Trim
                mstrTexto03 = rdBuscar("Texto03").ToString.Trim
                mstrTexto04 = rdBuscar("Texto04").ToString.Trim
                mstrTexto05 = rdBuscar("Texto05").ToString.Trim
            Else
                mdblKilometros = 0
                mintcidValor01 = 0
                mstrcCodigoV01 = ""
                mintcidOpto = 0
                mstrImagen = ""
                mdblImporte01 = 0
                mdblImporte02 = 0
                mdblImporte03 = 0
                mdblImporte04 = 0
                mdblImporte05 = 0
                mstrTexto01 = ""
                mstrTexto02 = ""
                mstrTexto03 = ""
                mstrTexto04 = ""
                mstrTexto05 = ""
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try

    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Rutas (IdRuta," & _
                            "Nombre," & _
                            "Kilometros," & _
                            "cIdValor01," & _
                            "cCodigoV01," & _
                            "cidOpto," & _
                            "Imagen," & _
                            "Importe01," & _
                            "Importe02," & _
                            "Importe03," & _
                            "Importe04," & _
                            "Importe05," & _
                            "Texto01," & _
                            "Texto02," & _
                            "Texto03," & _
                            "Texto04," & _
                            "Texto05) " & _
                            "VALUES (" & _
                            "@IdRuta," & _
                            "@Nombre," & _
                            "@Kilometros," & _
                            "@cIdValor01," & _
                            "@cCodigoV01," & _
                            "@cidOpto," & _
                            "@Imagen," & _
                            "@Importe01," & _
                            "@Importe02," & _
                            "@Importe03," & _
                            "@Importe04," & _
                            "@Importe05," & _
                            "@Texto01," & _
                            "@Texto02," & _
                            "@Texto03," & _
                            "@Texto04," & _
                            "@Texto05) "


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdRuta", mintIdRuta)
        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Kilometros", mdblKilometros)
        Command.Parameters.AddWithValue("cidValor01", mintcidValor01)
        Command.Parameters.AddWithValue("cCodigoV01", mstrcCodigoV01)
        Command.Parameters.AddWithValue("cidOpto", mintcidOpto)
        Command.Parameters.AddWithValue("Imagen", mstrImagen)
        Command.Parameters.AddWithValue("Importe01", mdblImporte01)
        Command.Parameters.AddWithValue("Importe02", mdblImporte02)
        Command.Parameters.AddWithValue("Importe03", mdblImporte03)
        Command.Parameters.AddWithValue("Importe04", mdblImporte04)
        Command.Parameters.AddWithValue("Importe05", mdblImporte05)
        Command.Parameters.AddWithValue("Texto01", mstrTexto01)
        Command.Parameters.AddWithValue("Texto02", mstrTexto02)
        Command.Parameters.AddWithValue("Texto03", mstrTexto03)
        Command.Parameters.AddWithValue("Texto04", mstrTexto04)
        Command.Parameters.AddWithValue("Texto05", mstrTexto05)

        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar la ruta " & mstrNombre, MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()
        Dim strSqlQry As String
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        strSqlQry = "Update Rutas Set " & _
                    " Nombre=@Nombre, " & _
                    " Kilometros=@Kilometros, " & _
                    " cCodigoV01=@cCodigoV01, " & _
                    " cidOpto=@cidOpto, " & _
                    " Imagen=@Imagen, " & _
                    " Importe01=@Importe01, " & _
                    " Importe02=@Importe02, " & _
                    " Importe03=@Importe03, " & _
                    " Importe04=@Importe04, " & _
                    " Importe05=@Importe05, " & _
                    " Texto01=@Texto01, " & _
                    " Texto04=@Texto04," & _
                    " Texto05=@Texto05 " & _
                    " where idRuta=@idRuta"

        Dim Command As New SqlCommand(strSqlQry, cnConn)
        Command.Parameters.AddWithValue("IdRuta", mintIdRuta)
        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Kilometros", mdblKilometros)
        Command.Parameters.AddWithValue("cidValor01", mintcidValor01)
        Command.Parameters.AddWithValue("cCodigoV01", mstrcCodigoV01)
        Command.Parameters.AddWithValue("cidOpto", mintcidOpto)
        Command.Parameters.AddWithValue("Imagen", mstrImagen)
        Command.Parameters.AddWithValue("Importe01", mdblImporte01)
        Command.Parameters.AddWithValue("Importe02", mdblImporte02)
        Command.Parameters.AddWithValue("Importe03", mdblImporte03)
        Command.Parameters.AddWithValue("Importe04", mdblImporte04)
        Command.Parameters.AddWithValue("Importe05", mdblImporte05)
        Command.Parameters.AddWithValue("Texto01", mstrTexto01)
        Command.Parameters.AddWithValue("Texto02", mstrTexto02)
        Command.Parameters.AddWithValue("Texto03", mstrTexto03)
        Command.Parameters.AddWithValue("Texto04", mstrTexto04)
        Command.Parameters.AddWithValue("Texto05", mstrTexto05)

        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar la ruta " & mstrNombre, MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
End Class
